package pwd.allen.construct.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂角色
 * @author lenovo
 * @create 2021-05-21 16:44
 **/
public class FlyWeightFactory {
    private Map<String, IFlyWeight> flyWeightMap = new HashMap<>();

    public IFlyWeight getFlyWeight(String key) {
        IFlyWeight flyweight = flyWeightMap.get(key);
        if (flyweight != null) {
            System.out.println("具体享元" + key + "已经存在，被成功获取！");
        } else {
            flyweight = new ConcreteFlyWeight(key);
            flyWeightMap.put(key, flyweight);
        }
        return flyweight;
    }
}
